# Redis和Token测试脚本

Write-Host "========== 聚合码Token问题诊断脚本 ==========" -ForegroundColor Cyan
Write-Host ""

# 1. 停止现有进程
Write-Host "[1/5] 停止现有Java进程..." -ForegroundColor Yellow
Get-Process | Where-Object {$_.ProcessName -like "*java*"} | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2

# 2. 重新编译
Write-Host "[2/5] 重新编译项目..." -ForegroundColor Yellow
mvn compile -DskipTests -pl ruoyi-framework,ruoyi-system,ruoyi-admin -q

# 3. 启动服务
Write-Host "[3/5] 启动服务..." -ForegroundColor Yellow
$job = Start-Job -ScriptBlock {
    cd D:\juhemajar\ruoyi-admin
    mvn spring-boot:run 2>&1 | Out-Null
}

# 4. 等待启动
Write-Host "[4/5] 等待服务启动（45秒）..." -ForegroundColor Yellow
Start-Sleep -Seconds 45

# 5. 开始测试
Write-Host "[5/5] 开始测试..." -ForegroundColor Yellow
Write-Host ""

# 测试1: Redis连接测试
Write-Host "========== 测试1: Redis连接 ==========" -ForegroundColor Cyan
try {
    $response = Invoke-WebRequest -Uri "http://localhost:8080/agg/test/redis" -Method GET
    $result = $response.Content | ConvertFrom-Json
    Write-Host "✅ Redis连接成功" -ForegroundColor Green
    Write-Host "  - 写入测试: $($result.data.'写入测试')" -ForegroundColor White
    Write-Host "  - 读取测试: $($result.data.'读取测试')" -ForegroundColor White
    Write-Host "  - Token数量: $($result.data.'Token数量')" -ForegroundColor White
} catch {
    Write-Host "❌ Redis连接失败: $_" -ForegroundColor Red
}
Write-Host ""

# 测试2: 用户登录
Write-Host "========== 测试2: 用户登录 ==========" -ForegroundColor Cyan
try {
    $loginData = @{
        userName = "13800138000"
        passWord = "123456"
    } | ConvertTo-Json
    
    $response = Invoke-WebRequest -Uri "http://localhost:8080/agg/user/login" `
        -Method POST `
        -Headers @{"Content-Type"="application/json"} `
        -Body $loginData
    
    $result = $response.Content | ConvertFrom-Json
    
    if ($result.code -eq 0) {
        Write-Host "✅ 登录成功" -ForegroundColor Green
        $userId = $result.data.id
        $token = $result.data.token
        Write-Host "  - 用户ID: $userId" -ForegroundColor White
        Write-Host "  - Token: $token" -ForegroundColor White
        
        # 测试3: 检查Token是否在Redis
        Write-Host ""
        Write-Host "========== 测试3: 验证Token存储 ==========" -ForegroundColor Cyan
        try {
            $checkResponse = Invoke-WebRequest -Uri "http://localhost:8080/agg/test/token?userId=$userId" -Method GET
            $checkResult = $checkResponse.Content | ConvertFrom-Json
            
            if ($checkResult.data.存在) {
                Write-Host "✅ Token已存储到Redis" -ForegroundColor Green
                Write-Host "  - Redis Key: $($checkResult.data.redisKey)" -ForegroundColor White
                Write-Host "  - Redis Value: $($checkResult.data.token)" -ForegroundColor White
            } else {
                Write-Host "❌ Token未存储到Redis" -ForegroundColor Red
            }
        } catch {
            Write-Host "❌ Token检查失败: $_" -ForegroundColor Red
        }
        
        # 测试4: 使用Token访问需要认证的接口
        Write-Host ""
        Write-Host "========== 测试4: Token认证测试 ==========" -ForegroundColor Cyan
        try {
            $authResponse = Invoke-WebRequest -Uri "http://localhost:8080/agg/user/detail?id=$userId" `
                -Method GET `
                -Headers @{"Authorization"="Bearer $token"}
            
            Write-Host "✅ Token认证成功" -ForegroundColor Green
            Write-Host "  - 响应: $($authResponse.Content.Substring(0, [Math]::Min(100, $authResponse.Content.Length)))..." -ForegroundColor White
        } catch {
            Write-Host "❌ Token认证失败: $_" -ForegroundColor Red
        }
        
    } else {
        Write-Host "❌ 登录失败: $($result.msg)" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ 登录请求失败: $_" -ForegroundColor Red
}

Write-Host ""
Write-Host "========== 测试完成 ==========" -ForegroundColor Cyan
Write-Host "请查看后端控制台日志获取详细信息" -ForegroundColor Yellow
Write-Host ""

# 停止后台任务
Write-Host "提示: 使用以下命令停止服务：" -ForegroundColor Gray
Write-Host '  Get-Job | Stop-Job; Get-Job | Remove-Job' -ForegroundColor Gray

